﻿using System;
using System.Drawing;
using System.Windows.Forms;
using StockControl;
using StockLib;
using StockUI.Properties;

namespace StockUI
{
    public partial class DialogOptionTab : DialogAbstract
    {
        public DialogOptionTab()
        {
            InitializeComponent();

            imageList.Images.Add(Resources.folder);
            imageList.Images.Add(Resources.doc);

            listSelect.PaintItem = delegate(ListViewItem item, object data)
            {
                StockTab tab = data as StockTab;

                item.Text = tab.Name;
                item.Tag = tab;
                item.ImageIndex = (tab.BlockId.EndsWith(".")) ? 0 : 1;
            };

            StockTabContainer.Instance.DataChanged += new StockTabContainerDataEventHandler(StockTabContainerInstance_DataChanged);
        }

        private void StockTabContainerInstance_DataChanged(object sender, EventArgs e)
        {
            listSelect.Bind();
        }

        private void DialogOptionTab_Load(object sender, System.EventArgs e)
        {
            TreeNode node = new TreeNode(StockBlockGroupCustom.BLOCK_GROUP_NAME);
            node.Tag = StockBlockGroupCustom.BLOCK_GROUP_ID;
            node.ImageIndex = node.SelectedImageIndex = 0;
            treeAll.Nodes.Add(node);
            foreach (StockBlock domain in StockBlockGroupCustom.Instance.Values)
            {
                    TreeNode node2 = new TreeNode(domain.Name);
                    node2.Tag = domain.BlockId;
                    node2.ImageIndex = node2.SelectedImageIndex = 1;
                    node.Nodes.Add(node2);
            }

            node = new TreeNode(StockBlockGroupClass.BLOCK_GROUP_NAME);
            node.Tag = StockBlockGroupClass.BLOCK_GROUP_ID;
            node.ImageIndex = node.SelectedImageIndex = 0;
            treeAll.Nodes.Add(node);
            foreach (StockBlock domain in StockBlockGroupClass.Instance.Values)
            {
                TreeNode node2 = new TreeNode(domain.Name);
                node2.Tag = domain.BlockId;
                node2.ImageIndex = node2.SelectedImageIndex = 1;
                node.Nodes.Add(node2);
            }

            foreach (string blockClass in StockBlockGroupRegular.Instance.Groups)
            {
                node = new TreeNode(blockClass);
                node.Tag = node.Text + ".";
                node.ImageIndex = node.SelectedImageIndex = 0;
                treeAll.Nodes.Add(node);
                foreach (StockBlock domain in StockBlockGroupRegular.Instance.Values)
                {
                    if (domain.GroupName == blockClass)
                    {
                        TreeNode node2 = new TreeNode(domain.Name);
                        node2.Tag = domain.BlockId;
                        node2.ImageIndex = node2.SelectedImageIndex = 1;
                        node.Nodes.Add(node2);
                    }
                }
            }
            treeAll.ExpandAll();
            treeAll.SelectedNode = treeAll.Nodes[0];

            listSelect.DataSource = StockTabContainer.Instance;
        }

        private void btnAdd_Click(object sender, System.EventArgs e)
        {
            if (!CheckLeftSelected())
                return;

            string blockId = Convert.ToString(treeAll.SelectedNode.Tag);
            if (CheckRepeat(blockId))
                return;

            StockTab tab = new StockTab();
            tab.Name = treeAll.SelectedNode.Text;
            tab.BlockId = Convert.ToString(treeAll.SelectedNode.Tag);
            StockTabContainer.Instance.Add(tab);
            StockTabContainer.Instance.RaiseDataChanged(this, EventArgs.Empty);
        }

        private void treeAll_DoubleClick(object sender, EventArgs e)
        {
            btnAdd_Click(sender, e);
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (!CheckRightSelected())
                return;

            for (int i = listSelect.SelectedItems.Count - 1; i >= 0; i--)
            {
                if (listSelect.Items.Count > 1)
                {
                    StockTabContainer.Instance.RemoveAt(listSelect.SelectedIndices[i]);
                }
            }
            StockTabContainer.Instance.RaiseDataChanged(this, EventArgs.Empty);
        }

        private void listSelect_DoubleClick(object sender, EventArgs e)
        {
            btnRemove_Click(sender, e);
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            if (!CheckRightSelected())
                return;

            foreach (ListViewItem item in listSelect.SelectedItems)
            {
                if (item.Index > 0)
                {
                    StockTab tabPrev = StockTabContainer.Instance[item.Index - 1];
                    StockTabContainer.Instance.RemoveAt(item.Index - 1);
                    StockTabContainer.Instance.Insert(item.Index, tabPrev);
                }
            }
            StockTabContainer.Instance.RaiseDataChanged(this, EventArgs.Empty);
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            if (!CheckRightSelected())
                return;

            foreach (ListViewItem item in listSelect.SelectedItems)
            {
                if (item.Index < listSelect.Items.Count - 1)
                {
                    StockTab tabNext = StockTabContainer.Instance[item.Index + 1];
                    StockTabContainer.Instance.RemoveAt(item.Index + 1);
                    StockTabContainer.Instance.Insert(item.Index, tabNext);
                }
            }
            StockTabContainer.Instance.RaiseDataChanged(this, EventArgs.Empty);
        }

        private bool CheckRepeat(string blockId)
        {
            foreach (ListViewItem item in listSelect.Items)
            {
                if ((item.Tag as StockTab).BlockId == blockId)
                {
                    //MessageBox.Show(this, "节点已经添加！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return true;
                }
            }

            return false;
        }

        private bool CheckLeftSelected()
        {
            if (treeAll.SelectedNode == null)
            {
                MessageBox.Show(this, "请选择左侧节点", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            return true;
        }

        private bool CheckRightSelected()
        {
            if (listSelect.SelectedItems.Count <= 0)
            {
                MessageBox.Show(this, "请选择右侧节点", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            return true;
        }

        private void treeAll_ItemDrag(object sender, ItemDragEventArgs e)
        {
            if (e.Item != null)
            {
                TreeNode node = e.Item as TreeNode;

                StockTab tab = new StockTab();
                tab.Name = node.Text;
                tab.BlockId = Convert.ToString(node.Tag);
                listSelect.DoDragDrop(tab, DragDropEffects.Move);
            }
        }

        private void listSelect_ItemDrag(object sender, ItemDragEventArgs e)
        {
            if (listSelect.SelectedItems.Count > 0)
            {
                int selectedIndex = listSelect.SelectedItems[0].Index;
                listSelect.DoDragDrop(StockTabContainer.Instance[selectedIndex], DragDropEffects.Move);
            }
        }

        private void listSelect_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(StockTab)))
            {
                e.Effect = DragDropEffects.Move;
            }
        }

        private void listSelect_DragDrop(object sender, DragEventArgs e)
        {
            Point p = listSelect.PointToClient(new Point(e.X, e.Y));
            ListViewItem itemPut = listSelect.GetItemAt(p.X, p.Y);

            if (e.Data.GetDataPresent(typeof(StockTab)))
            {
                StockTab tabDrag = e.Data.GetData(typeof(StockTab)) as StockTab;
                if (tabDrag == null)
                    return;

                int indexDrag = StockTabContainer.Instance.IndexOf(tabDrag);
                if (indexDrag >= 0)
                {
                    if (itemPut == null)
                        return;                   

                    int indexPut = listSelect.Items.IndexOf(itemPut);
                    if (indexPut == indexDrag)
                        return;

                    StockTabContainer.Instance.Remove(tabDrag);
                    StockTabContainer.Instance.Insert(indexPut, tabDrag);
                }
                else
                {
                    if (CheckRepeat(tabDrag.BlockId))
                        return;

                    if (itemPut == null)
                    {
                        StockTabContainer.Instance.Add(tabDrag);
                    }
                    else
                    {
                        int indexPut = listSelect.Items.IndexOf(itemPut);
                        StockTabContainer.Instance.Insert(indexPut, tabDrag);
                    }
                }
                StockTabContainer.Instance.RaiseDataChanged(this, EventArgs.Empty);
            }
        }

        private void treeAll_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(StockTab)))
            {
                e.Effect = DragDropEffects.Move;
            }
        }

        private void treeAll_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(StockTab)))
            {
                StockTab tabDrag = e.Data.GetData(typeof(StockTab)) as StockTab;
                if (StockTabContainer.Instance.Contains(tabDrag))
                {
                    StockTabContainer.Instance.Remove(tabDrag);
                    StockTabContainer.Instance.RaiseDataChanged(this, EventArgs.Empty);
                }
            }
        }
    }
}
